﻿#pragma once
#include <atomic>
#include <cstdint>
#include "DeviceInterface.h"
#include "Network.h"
#include "pch.h"
#include <string>
#include <unordered_map>

#define SDR_HEAD_1  0xA5
#define SDR_HEAD_2  0x5A

#define SDR_COMMAND_HEARTBEAT 2002
#define SDR_COMMAND_OBJECT 2004

class SdrRadarClient : public DeviceInterface {

#pragma pack(push, 1)
   
struct SDR_Frame
{
	uint16_t Head;
	uint16_t Length;
	uint16_t Commmand;
	// uint8_t Data[];
};


struct SDR_Object
{
	uint16_t ID;
	uint16_t X;
	uint16_t Y;
	int16_t Angle;
	int16_t VelocityX;
	int16_t VelocityY;
	int16_t AccelerationX;
	int16_t AccelerationY;
	uint8_t Reserved1;
	//latitude and longitude
	double Longitude;
	uint8_t Reserved2;
	uint8_t Reserved3;
	double Latitude;
	uint8_t Lane;
	uint8_t End = 0xF0;
};

struct SDR_Objects
{
	uint8_t Time[8];
	uint16_t Frame;
	uint16_t Count;
	SDR_Object Object[];
};


#pragma pack(pop)

public:
    virtual void Init(std::shared_ptr<DeviceInfo> info);
    virtual void Start();
    virtual void Stop();

private:
    void OnConnected();
    void OnReceiveData(const toolkit::Buffer::Ptr &buf);
    void ParseData();

private:
    std::shared_ptr<TcpClient> _client = nullptr;
    std::string _buffer = "";
    std::shared_ptr<DeviceInfo> _device_info = nullptr;
};